package com.seth.myapplication.surface;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.seth.myapplication.DataApp;
import com.seth.myapplication.LoginActivity;
import com.seth.myapplication.MainActivity;
import com.seth.myapplication.MyTaskActivity;
import com.seth.myapplication.R;
import com.seth.myapplication.UsrInfoActivity;
import com.seth.myapplication.bean.User;
import com.seth.myapplication.dialog.ImagePickerFragment;
import com.seth.myapplication.service.NetWorkService;
import com.seth.myapplication.utils.SqlDao;

import java.io.File;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Seth on 2017/10/11.
 */

public class page4Fragment extends Fragment {
    private static final int LOGIN_RESULT =2;
    private static final int UPDATE_RESULT =1;
    public static final int UPDATE_FROG =5;
    public static final String UPDATE_NET_FROG = "com.seth.myapplication.surface.RECEIVE.page4Fragment";
    private CircleImageView frogImg;
    private View view;
    private TextView userNick;
    private TextView userInte;
    private Button login_btn;
    private TextView myTask;
    private TextView myReword;
    private User mUser = null;
    private ImageView moreUsrInfo;
    private DataApp dataApp;
    private UserBroadCast msgReceiver;
    private Button unRegisterBtn;

    private File tempFile;
    private Bitmap frog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.forth_page_fragment, container, false);
        dataApp = (DataApp) getActivity().getApplication();
        mUser = dataApp.getUser();

        initView(view);
        initEvent();


        return view;

    }

    @Override
    public void onAttach(Context context) {

        //动态注册广播接收器
        msgReceiver = new UserBroadCast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(UPDATE_NET_FROG);
        context.registerReceiver(msgReceiver, intentFilter);
        super.onAttach(context);
    }


    private void initEvent() {

        if(mUser!=null){
            login_btn.setVisibility(View.GONE);
            userNick.setText(mUser.getNickname());
        }else{
            unRegisterBtn.setVisibility(View.GONE);
        }
        unRegisterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataApp.setUser(null);
                int rs = new SqlDao(getContext()).delUsr();
                Log.i("SQLiteresult",rs+"");
                unRegisterBtn.setVisibility(View.GONE);
                login_btn.setVisibility(View.VISIBLE);
                userNick.setText("未登录");
                userInte.setText("积分:0");
                frogImg.setImageResource(R.mipmap.exm2);
            }
        });

        frogImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mUser!=null){
                    ImagePickerFragment dialog =ImagePickerFragment.NewInstace(0,mUser.getPhoneNum());
                    dialog.setTargetFragment(page4Fragment.this,UPDATE_FROG );
                    dialog.show(getFragmentManager(), "选择图片来源");
                }else {
                    Toast.makeText(getContext(), "请先登陆", Toast.LENGTH_SHORT).show();
                }
            }
        });

        moreUsrInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mUser==null){
                    Toast.makeText(getContext(), "请先登陆", Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = UsrInfoActivity.newIntent(getContext(), mUser);
                    startActivityForResult(intent, UPDATE_RESULT);
                }
            }
        });

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);

                startActivityForResult(intent,LOGIN_RESULT);
            }
        });

        myReword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mUser==null){
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = MyTaskActivity.newIntent(getContext(),mUser,1);
                    getActivity().startActivity(intent);
                }
            }
        });

        myTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mUser==null){
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = MyTaskActivity.newIntent(getContext(),mUser,0);
                    getActivity().startActivity(intent);
                }
            }
        });

    }

    private void initView(View view) {
        moreUsrInfo = (ImageView) view.findViewById(R.id.more_usr_info);
        login_btn = (Button) view.findViewById(R.id.btn_login);
        myTask = (TextView) view.findViewById(R.id.my_task);
        userNick = (TextView) view.findViewById(R.id.usr_nick);
        userInte = (TextView) view.findViewById(R.id.usr_inte);
        frogImg = (CircleImageView) view.findViewById(R.id.usr_photo);
        myReword = (TextView) view.findViewById(R.id.my_rewords);
        unRegisterBtn = (Button) view.findViewById(R.id.unregister_btn);
    }




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("login","收到注册反馈");
        switch (resultCode){
            case 4:
                mUser = (User) data.getSerializableExtra("user");
                Log.i("login",mUser.getNickname());
                if(mUser!=null){
                    Log.i("login","收到注册反馈并更新");
                    userNick.setText(mUser.getNickname());
                    dataApp.setUser(mUser);
                    login_btn.setVisibility(View.INVISIBLE);
                    unRegisterBtn.setVisibility(View.VISIBLE);
                }
                break;
            case UPDATE_FROG:
                Log.i("login","测试有没有收到");
                tempFile = (File) data.getSerializableExtra("file");
                Log.i("FROG_SET_RESULT",tempFile.getAbsolutePath());
                Bitmap bitmap =  BitmapFactory.decodeFile(tempFile.getAbsolutePath());
                frogImg.setImageBitmap(bitmap);
                onResume();
                break;
        }
    }

    public void setTempFile(File tempFile) {
        this.tempFile = tempFile;
        Log.i("FROG_SET_RESULT",tempFile.getAbsolutePath());
        new NetWorkService(getActivity()).updateFrog(mUser.getUsrId(),tempFile);

    }

    public void setFrog(Bitmap frog) {
        this.frog = frog;
        frogImg.setImageBitmap(frog);
    }


    private class UserBroadCast extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String res = intent.getStringExtra("result");
            Bitmap bitmap =null;
            if("image".equals(res)){
                bitmap = intent.getParcelableExtra("bitmap");
                setFrog(bitmap);

            }
        }
    }
}
